package com.bolt.admin.config;

import com.bolt.support.cache.redis.RedisManager;
import com.bolt.support.captcha.CaptchaCacheService;
import com.bolt.support.captcha.RedisCaptchaCacheService;
import com.bolt.support.query.nativequery.NQuery;
import com.bolt.support.spring.jpa.jpql.JPAQueryFactory;
import com.bolt.support.sqlconfig.builder.SqlTemplateFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.persistence.EntityManager;
import java.io.IOException;

/**
 * Created by Administrator on 2019/1/11.
 */
@Configuration
public class BoltConfiguration {

    @Bean
    public SqlTemplateFactory createSqlTemplateBuilder() throws IOException {
        SqlTemplateFactory builder = new SqlTemplateFactory();
        builder.setSqlMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:sqlconfig/sqlconfig*.xml"));
        return builder;
    }

//    @Bean
//    public OpenEntityManagerInViewFilter openEntityManagerInViewFilter() {
//        return new OpenEntityManagerInViewFilter();
//    }

    @Bean
    public NQuery createNQuery(SqlTemplateFactory sqlTemplateFactory, EntityManager entityManager) {
        return new NQuery(entityManager, sqlTemplateFactory);
    }

    @Bean
    public JPAQueryFactory createJPAQueryFactory(EntityManager entityManager) {
        return new JPAQueryFactory(entityManager);
    }


    @Bean
    public CaptchaCacheService createCaptchaCacheService(RedisManager redisManager) {
        RedisCaptchaCacheService redisCaptchaCacheService = new RedisCaptchaCacheService(redisManager);
        return redisCaptchaCacheService;
    }


}
